package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.ficase.FiImpReqVo;
import com.miplus.ccrm.core.controller.vo.ficase.FiImpResVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.FiImportHistMapper;
import com.miplus.ccrm.core.service.conf.SysNamesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author pc
 * @description 针对表【fi_import_hist(催收系统批量操作历史)】的数据库操作Service实现
 * @createDate 2024-04-20 21:50:38
 */
@Service
public class FiImportHistServiceImpl extends MPJBaseServiceImpl<FiImportHistMapper, FiImportHist>
        implements FiImportHistService{

    @Resource
    private SysNamesService sysNamesService;

    @Override
    public IPage<FiImpResVo> fiImportBatchList(FiImpReqVo paras) {
        List<String> templist = null;
        if (JPubUtils.IsNotEmpty(paras.getTemptype())){
            templist=sysNamesService.getNamesID("FIDATA.TEMP", paras.getTemptype());
        }

        if(JPubUtils.IsEqual(paras.getDotype(),"export")){
            return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                    FiImpResVo.class,
                    new MPJLambdaWrapper<FiImportHist>()
                            .selectAs("t.tempid", FiImpResVo::getTempdsc)
                            .selectAll(FiImportHist.class)
                            .eq(JPubUtils.IsNotEmpty(paras.getDotype()), FiImportHist::getDotype, paras.getDotype())
                            .eq(JPubUtils.IsNotEmpty(paras.getBatchid()),FiImportHist::getBatchid,paras.getBatchid())
                            .like(JPubUtils.IsNotEmpty(paras.getTempid()), FiImportHist::getTempid, paras.getTempid())
                            .between(JPubUtils.IsNotEmpty(paras.getStartdt()), FiImportHist::getDodt, JPubUtils.StringToDate(paras.getStartdt()), JPubUtils.StringToDate(paras.getEnddt()))
                            .orderByDesc(FiImportHist::getDodt)
            );
        }else {
            return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                    FiImpResVo.class,
                    new MPJLambdaWrapper<FiImportHist>()
                            .selectAs("fun_getnames('FIDATA.TEMP',t.tempid)", FiImpResVo::getTempdsc)
//                            .selectAs("fun_getnames('FI.IMPORT.TYPE',t.imptype)", FiImpResVo::getImptypedsc)
//                            .selectAs("(select cp.name from cp_calllist cp where cp.clid=t.clid)", FiImpResVo::getClidname)
                            .selectAll(FiImportHist.class)
                            .eq(JPubUtils.IsNotEmpty(paras.getDotype()), FiImportHist::getDotype, paras.getDotype())
                            .eq(JPubUtils.IsNotEmpty(paras.getTempid()), FiImportHist::getTempid, paras.getTempid())
                            .eq(JPubUtils.IsNotEmpty(paras.getBatchid()),FiImportHist::getBatchid,paras.getBatchid())
  //                          .eq(JPubUtils.IsNotEmpty(paras.getImptype()),FiImportHist::getImptype,paras.getImptype())
                            .between(JPubUtils.IsNotEmpty(paras.getStartdt()), FiImportHist::getDodt, JPubUtils.StringToDate(paras.getStartdt()), JPubUtils.StringToDate(paras.getEnddt()))
                            .in(JPubUtils.IsNotEmpty(templist), FiImportHist::getTempid, templist)
                            .orderByDesc(FiImportHist::getDodt)
            );
        }
    }

//    @Override
//    public void AddImportHist(String batchid,String batchdsc,String $usrid,
//                               String tempid,String filename,int totlecnt,int okcnt)
//    {
//        FiImportHist fiImportHist=new FiImportHist();
//        fiImportHist.setBatchid(batchid);
//        fiImportHist.setBatchdesc(batchdsc);
//        fiImportHist.setDotype("import");
//        fiImportHist.setTempid(tempid);
//        if(JPubUtils.IsNotEmpty(filename)) {
//            fiImportHist.setFilename(filename);
//        }
//        fiImportHist.setDousr($usrid);
//        fiImportHist.setDodt(new Date());
//        fiImportHist.setTotalcnt(totlecnt);
//        fiImportHist.setOkcnt(okcnt);
//        fiImportHist.setStatus(1);
//        baseMapper.insert(fiImportHist);
//    }


    @Override
    public int AddImportHist(String batchid,String batchdsc,String $usrid,
                             String tempid,String filename,int totlecnt,int okcnt)
    {
        return AddImportHist(batchid,batchdsc,$usrid,tempid,filename,totlecnt,okcnt,null);
    }

    @Override
    public int AddImportHist(String batchid,String batchdsc,String $usrid,
                             String tempid,String filename,int totlecnt,int okcnt,String clid)
    {
        return AddImportHist(batchid,batchdsc,$usrid,1,tempid,filename,totlecnt,okcnt,clid);
    }

    @Override
    public int AddImportHist(String batchid,String batchdsc,String $usrid,Integer status,
                             String tempid,String filename,int totlecnt,int okcnt,String clid)
    {
        return AddImportHist(batchid,batchdsc,$usrid,status,tempid,filename,totlecnt,okcnt,clid,null);
    }

    @Override
    public int AddImportHist(String batchid,String batchdsc,String $usrid,Integer status,
                             String tempid,String filename,int totlecnt,int okcnt,String clid,String imptype)
    {
        FiImportHist fiImportHist=new FiImportHist();
        fiImportHist.setBatchid(batchid);
        fiImportHist.setBatchdesc(batchdsc);
        fiImportHist.setDotype("import");
        fiImportHist.setTempid(tempid);
        if(JPubUtils.IsNotEmpty(filename)) {
            fiImportHist.setFilename(filename);
        }
        fiImportHist.setDousr($usrid);
        fiImportHist.setDodt(new Date());
        fiImportHist.setTotalcnt(totlecnt);
        fiImportHist.setOkcnt(okcnt);
        fiImportHist.setStatus(status);
//        if(JPubUtils.IsNotEmpty(clid)) {
//            fiImportHist.setClid(clid);
//        }
//        if(JPubUtils.IsNotEmpty(imptype)){
//            fiImportHist.setImptype(imptype);
//        }
        return baseMapper.insert(fiImportHist);
    }


    @Override
    public int UpImportHist(String batchid,int okcnt)
    {
        return baseMapper.update(null,new UpdateWrapper<FiImportHist>()
                .lambda()
                .set(FiImportHist::getOkcnt,okcnt)
                .eq(FiImportHist::getBatchid,batchid)
        );
    }
    @Override
    public int AddExportHist(String batchid,String $usrid,String tempid,String filename,int okcnt)
    {
        FiImportHist fiImportHist=new FiImportHist();
        fiImportHist.setBatchid(batchid);
        fiImportHist.setBatchdesc(tempid+batchid);
        fiImportHist.setDotype("export");
        fiImportHist.setTempid(tempid);
        if(JPubUtils.IsNotEmpty(filename)) {
            fiImportHist.setFilename(filename);
        }
        fiImportHist.setDousr($usrid);
        fiImportHist.setDodt(new Date());
        fiImportHist.setTotalcnt(okcnt);
        fiImportHist.setOkcnt(okcnt);
        fiImportHist.setStatus(1);
        return baseMapper.insert(fiImportHist);
    }
}




